package com.lj.listener;

import com.lj.dao.SqliteHelper;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Properties;

/**
 * Created by dave_suwei on 2017/12/9.
 */
public class MyServletContextListener implements ServletContextListener {
    /**
     * 关闭操作
     */
    public void contextDestroyed(ServletContextEvent sc) {
        System.out.println("------------------ Tomcat关闭! ------------------");
    }

    /**
     * 启动初始化
     */
    public void contextInitialized(ServletContextEvent sc) {
        System.out.println("------------------ Tomcat启动! ------------------");
        //创建数据表
        Properties properties=new Properties();
        String dbPath=null;
        try {
            properties.load(getClass().getResourceAsStream("/application.properties"));
            dbPath=properties.getProperty("database.path");
            String dirPath=dbPath.substring(0,dbPath.lastIndexOf("/"));
            System.out.println("dirPath->"+dirPath);
            File file=new File(dirPath);
            if (file.exists()){
                if(!file.isDirectory()){
                    dbPath=null;
                }
            }else{
                dbPath=null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(dbPath==null){
            dbPath=sc.getServletContext().getRealPath("/")+"unburden.db";
        }
        System.out.println("dbPath->"+dbPath);
        SqliteHelper sh=null;
        try {
            sh=new SqliteHelper(dbPath);
            //参数表
            sh.executeUpdate("CREATE TABLE IF NOT EXISTS \"t_params_info\" (\n" +
                    "\"id\"  INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\n" +
                    "\"showTime\"  INTEGER DEFAULT 0,\n" +
                    "\"lastSelect\"  INTEGER DEFAULT 0,\n" +
                    "\"showCount\"  INTEGER DEFAULT 0,\n" +
                    "\"rejectCount\"  INTEGER DEFAULT 0,\n" +
                    "\"considerCount\"  INTEGER DEFAULT 0,\n" +
                    "\"startDate\"  TEXT,\n" +
                    "\"endDate\"  TEXT,\n" +
                    "\"rejectInterval\"  INTEGER DEFAULT 0,\n" +
                    "\"considerInterval\"  INTEGER DEFAULT 0\n" +
                    ");",
                    //消息表
                    "CREATE TABLE IF NOT EXISTS \"t_msg_info\" (\n" +
                            "\"id\"  INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\n" +
                            "\"createTime\"  INTEGER DEFAULT 0,\n" +
                            "\"receiveTime\"  INTEGER DEFAULT 0,\n" +
                            "\"status\"  INTEGER DEFAULT 0,\n" +
                            "\"type\"  INTEGER DEFAULT 0,\n" +
                            "\"sender\"  TEXT,\n" +
                            "\"receiver\"  TEXT,\n" +
                            "\"content\"  TEXT\n" +
                            ");");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if(sh!=null){
                sh.destroyed();
            }
        }
    }
}
